package cn.dnui_cr503.dnuifood_16110100503.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.CollectStatus;
import cn.dnui_cr503.dnuifood_16110100503.controller.FoodActivity;
import cn.dnui_cr503.dnuifood_16110100503.controller.FoodListActivity;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;

/**
 * Created by chengrui on 2018/12/2.
 */

public class CollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<CollectStatus> mDataList;
    private LayoutInflater mLayoutInflater;;

    public CollectAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.collect_item,parent,false);

        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, final int position) {
        final CollectStatus entity = (CollectStatus)mDataList.get(position);
        if (null == entity)
            return;
        CollectAdapter.ViewHolder holder = (CollectAdapter.ViewHolder) holder1;
        if (entity.getFlag().equals("0")){
            // holder.collect_shopid.setText(entity.getShop_id());
            holder.collect_name.setText(entity.getShopname());
            Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到店铺详细页
                    String str = String.valueOf(mDataList.get(position).getShop_id());
                    Intent intent = new Intent(mContext,FoodListActivity.class);
                    intent.putExtra("shop_id",str);
                    mContext.startActivity(intent);
                }
            });

        }else {
            // holder.collect_foodid.setText(entity.getFood_id());
            holder.collect_name.setText(entity.getFoodname());
            Picasso.with(mContext).load(UserModel.Constants.BASE_URL+entity.getPic()).into(holder.collect_pic);

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转到菜谱详细页
                    String str = String.valueOf(mDataList.get(position).getFood_id());
                    String str1 = String.valueOf(mDataList.get(position).getFoodname());
                    Intent intent = new Intent(mContext,FoodActivity.class);
                    intent.putExtra("shop_id",entity.getShop_id());
                    intent.putExtra("food_id",str);
                    intent.putExtra("foodname",str1);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mDataList==null)
            return 0;
        else
            return mDataList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView collect_pic;
        private TextView collect_name;

        public ViewHolder(View itemView) {
            super(itemView);
            collect_pic=(ImageView)itemView.findViewById(R.id.collect_image);
            collect_name=(TextView) itemView.findViewById(R.id.collect_name);
        }
    }
}
